﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Plugin.BLE.Abstractions.Contracts;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace MauiBluetooth.ViewModel
{
    /// <summary>
    /// 
    /// </summary>
    public class LinkeModel : ObservableObject
    {



        /// <summary>
        /// 蓝牙名称
        /// </summary>
        private IAdapter _adapter1;
        /// <summary>
        /// 蓝牙名称
        /// </summary>
        public IAdapter adapter1 { get => _adapter1; set { _adapter1 = value; OnPropertyChanged(); } }
        /// <summary>
        /// 蓝牙名称
        /// </summary>
        private IDevice _dev;
        /// <summary>
        /// 蓝牙名称
        /// </summary>
        public IDevice dev { get => _dev; set { _dev = value; OnPropertyChanged(); } }
        /// <summary>
        /// 发送信息
        /// </summary>
        private string _sendinfo;
        /// <summary>
        /// 发送信息
        /// </summary>
        public string sendinfo { get => _sendinfo; set { _sendinfo = value; OnPropertyChanged(); } }

        /// <summary>
        /// 发送信息
        /// </summary>
        private string _revalue;
        /// <summary>
        /// 发送信息
        /// </summary>
        public string revalue { get => _revalue; set { _revalue = value; OnPropertyChanged(); } }

        /// <summary>
        /// 服务
        /// </summary>
        private ObservableCollection<IService> _IService;
        /// <summary>
        /// 服务
        /// </summary>
        public ObservableCollection<IService> IService { get => _IService; set { _IService = value; OnPropertyChanged(); } }
        /// <summary>
        /// 服务
        /// </summary>
        private ObservableCollection<ICharacteristic> _ICharacteristic;
        /// <summary>
        /// 服务
        /// </summary>
        public ObservableCollection<ICharacteristic> ICharacteristic { get => _ICharacteristic; set { _ICharacteristic = value; OnPropertyChanged(); } }

        /// <summary>
        /// 
        /// </summary>
        public ICommand Link { get; set; }
        public ICommand Send { get; set; }
        public ICommand Read { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public LinkeModel()
        {
            IService = new ObservableCollection<IService>();
            ICharacteristic = new ObservableCollection<ICharacteristic>();
            Link = new AsyncRelayCommand(LinkMethod);
            Send = new AsyncRelayCommand(SendMethod);
            Read = new AsyncRelayCommand(ReadMethod);
        }

        private async Task ReadMethod()
        {
            IService.Clear();
            ICharacteristic.Clear();
            await adapter1.ConnectToDeviceAsync(dev);
           
            var s = await dev.GetServicesAsync();
            foreach (var item in s)
            {
                IService.Add(item);
                var cc = await item.GetCharacteristicsAsync();
                foreach (var ccc in cc)
                {
                    ICharacteristic.Add(ccc);
                    try
                    {
                        if (ccc.CanRead)
                        {
                            var data = await ccc.ReadAsync();
                            var dst = UTF8Encoding.UTF8.GetString(data.data);
                            revalue += dst;
                        }

                    }
                    catch (Exception e)
                    {
                        revalue += e.Message;
                    }
                }
            }
        }

        private async Task SendMethod()
        {

        }

        private async Task LinkMethod()
        {
            await adapter1.ConnectToDeviceAsync(dev);
        }
    }
}
